package weaver.interfaces.xfjt.action;


import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;


/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-10-10
 * @Description: 4.流程结束后，在【发放台帐】生成申领数量大于0的物品发放登记记录，扣减库存。
 * 5.计算领用物品的总价（和办公用品的计算逻辑一致）
 * 6.领用记录写入【食堂用品入库台账】明细表
 * @ModifyLog:
 **/
public class WeaStypSllcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        writeLog(">>>>>WeaStypSllcAction>>>>>requestid="+requestid);
        try {
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            //发放时间
            String ffsj = Util.null2String(rs.getString("ffsj"));
            //申领人
            String pdr = Util.null2String(rs.getString("pdr"));
            //申领日期
            String bcpdrq = Util.null2String(rs.getString("bcpdrq"));

            rs.executeQuery("select mc,lb,slsl from "+tablename+"_dt1 where mainid=?",mainid);
            while (rs.next()) {
                //名称
                String mc = Util.null2String(rs.getString("mc"));
                //申领数量
                int slsl = Util.getIntValue(rs.getString("slsl"));
                //申领数量
                String lb = Util.null2String(rs.getString("lb"));
                int xslsl = slsl;
                int count;
                rs1.executeQuery("select id,(sl-isnull(ylysl,0)) sysl from uf_styprktz " +
                        "where wpmc=? and wplb=? and (sfsc is null or sfsc=1) and (sl-isnull(ylysl,0))>0" +
                        " order by rksj", mc,lb);
                while (rs1.next()) {
                    int id = Util.getIntValue(rs1.getString("id"));
                    int sysl = Util.getIntValue(rs1.getString("sysl"),0);
                    rs1.writeLog(">>>>id="+id+",slsl="+slsl+",sysl="+sysl);
                    count = xslsl - sysl;
                    if (count > 0) {
                        xslsl = xslsl - sysl;
                        rs1.executeUpdate("insert into uf_styprktz_dt1(mainid,ffsj,slr,slrq,sllc,slsl) " +
                                "values (?,?,?,?,?,?)",id,ffsj,pdr,bcpdrq, requestid,sysl);
                        rs1.executeUpdate("update uf_styprktz set ylysl=isnull(ylysl,0)+"+sysl+" where id=?",id);
                    } else {
                        rs1.executeUpdate("insert into uf_styprktz_dt1(mainid,ffsj,slr,slrq,sllc,slsl) " +
                                "values (?,?,?,?,?,?)",id,ffsj,pdr,bcpdrq, requestid,xslsl);
                        rs1.executeUpdate("update uf_styprktz set ylysl=isnull(ylysl,0)+"+xslsl+" where id=?",id);
                        break;
                    }

                }
                rs.executeUpdate("update uf_stypkc set kc=kc-"+slsl+" where mc=?",mc);
            }
        return Action.SUCCESS;
    } catch(
    Exception e)

    {
        e.printStackTrace();
        requestInfo.getRequestManager().setMessageid("111111");
        requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
        return Action.FAILURE_AND_CONTINUE;
    }
}

}
